#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include <TlHelp32.h>

HANDLE hProcess = 0;
DWORD Process_ID;
DWORD GetPrcsIDbyName(LPCWSTR PrcsName);
DWORD Add_Func(DWORD BaseAdd, DWORD EA1, DWORD EA2, DWORD Num);

// 窗口置顶函数
void TopWindow(HWND& hWnd) {
    // SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    ::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}

// 根据进程id获取窗口句柄
HWND GetWindowHandleByPID(DWORD dwProcessID) {
    HWND h = GetTopWindow(0);
    while (h) {
        DWORD pid = 0;
        DWORD dwTheardId = GetWindowThreadProcessId(h, &pid);
        if (dwTheardId != 0) {
            if (pid == dwProcessID /*your process id*/) {
                // here h is the handle to the window

                if (GetTopWindow(h)) {
                    return h;
                }
                // return h;
            }
        }
        h = ::GetNextWindow(h, GW_HWNDNEXT);
    }
    return NULL;
}

BOOL EnumWindowss(HWND hwnd, LPARAM lParam) {
    DWORD pid = 0;
    GetWindowThreadProcessId(hwnd, &pid);
    if (pid == GetCurrentProcessId())  // 判断pid
    {
        char text[1024];
        GetWindowTextA(hwnd, (LPSTR)text, 1024);  // 必须含有标题文字
        if (strlen(text) != 0 && IsWindowVisible(hwnd)) {
            printf("%s\n", text);
            return FALSE;
        }
    }
    return TRUE;
};

int main(void) {
    // 设置控制台窗口大小
    system("mode con cols=40 lines=20");
    // 设置控制台字符编码格式
    // system("chcp 65001");

    // HWND hGame = FindWindow(NULL, L"Google Chrome");
    // printf("ddd%d\n", hGame);

    // system("setTitle");
    // if (hGame <= 0)
    // {
    //   printf("no find game\n");
    //   exit(0);
    // }
    // else
    // {
    //   printf("find");
    // }

    // 根据进程名称获取进程pid
    Process_ID = GetPrcsIDbyName(L"计算器.exe");
    printf("Process_ID: %d\n", Process_ID);

    // 根据pid获取进程句柄
    HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, Process_ID);
    printf("handle: %d\n", handle);

    // 获取当前窗口句柄
    // HWND hWnd = ::GetForegroundWindow();

    HWND hwnd = GetWindowHandleByPID(Process_ID);
    printf("hwnd: %d\n", hwnd);

    char text[1024];
    // 获取窗口标题
    GetWindowTextA(hwnd, (LPSTR)text, 1024);  // 必须含有标题文字
    if (strlen(text) != 0) {
        // 获得指定窗口的可视状态，即显示或者隐藏
        IsWindowVisible(hwnd);
    }
    printf("%s\n", text);

    while (1) {
        TopWindow(hwnd);
        Sleep(10);
    }

    system("pause");
    return 0;
}

DWORD GetPrcsIDbyName(LPCWSTR PrcsName) {
    HANDLE hSnapShot;
    PROCESSENTRY32 PE32;
    ZeroMemory(&PE32, sizeof(PE32));
    PE32.dwSize = sizeof(PE32);
    hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapShot == INVALID_HANDLE_VALUE) {
        return 0;
    }
    if (!Process32First(hSnapShot, &PE32)) {
        return 0;
    }
    do {
        if (lstrcmpi(PE32.szExeFile, PrcsName) == 0) {
            CloseHandle(hSnapShot);
            return PE32.th32ProcessID;
        }
    } while (Process32Next(hSnapShot, &PE32));
    CloseHandle(hSnapShot);
    return 0;
}